/**
 *  This file is part of AugeasWH.
 *
 *  AugeasWH. is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  AugeasWH. is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with AugeasWH.  If not, see <http://www.gnu.org/licenses/>.
 */

package cz.fit.vutbr.pis2012.augeaswh.control.authentication;

import cz.fit.vutbr.pis2012.augeaswh.persistence.User;

public class AccessRightManager {
	
	private AccessRightManager() { }
	
	public enum AccessRight {
		SUBJECTS, /* 1 */
		DISCOUNTS, /* 2 */
		ORDERS, /* 3 */
		STOCK_TYPES, /* 4 */
		ADMINISTRATION, /* 5 */
		SLOTS, /* 6 */
		STOCK_ITEMS, /* 7 */
		ORDERS_WHMAN, /* 8 */
		STOCK_ITEMS_LIST, /* 9 */
		INVALID /* inak */
	}
	
	public static AccessRight translateRightIdToAccessRight(int rightId) {
		switch (rightId) {
			case 1:
				return AccessRight.SUBJECTS;
			case 2:
				return AccessRight.DISCOUNTS;
			case 3:
				return AccessRight.ORDERS;
			case 4:
				return AccessRight.STOCK_TYPES;
			case 5:
				return AccessRight.ADMINISTRATION;
			case 6:
				return AccessRight.SLOTS;
			case 7:
				return AccessRight.STOCK_ITEMS;
			case 8:
				return AccessRight.ORDERS_WHMAN;
			case 9:
				return AccessRight.STOCK_ITEMS_LIST;
		}
		return AccessRight.INVALID;
	}
	
	/**
	 * Otestuje ci ma prihlaseny uzivatel pravo na danu akciu
	 * @param user uzivatel
	 * @param right pravo 
	 * @return true ak ma pravo, false v opacnom pripade
	 */
	public static boolean hasUserRightToPerformAction(User user, AccessRight right) {
		
		if (user == null)
			return false;
		
		// ADMIN
		if (user.getRole().equals("ADM")) {
			return hasAdminRight(right);
		} 
		
		// Ekonomicky pracovnik
		if (user.getRole().equals("PRC")) {
			return hasWorkerRight(right);
		} 
		
		// Skladnik
		if (user.getRole().equals("SKL")) {
			return hasWarehouseWorkerRight(right);
		} 
		
		return false;
	}
	
	/**
	 * Vrati informacie o tom, ci ma ADMIN prava na danu akciu
	 * @return true ak ma, inak false
	 */
	private static boolean hasAdminRight(AccessRight right) {
		return right == AccessRight.ADMINISTRATION;
	}
	
	/**
	 * Vrati informacie o tom, ci ma ADMIN prava na prezeranie danej stranky
	 * @return true ak ma, inak false
	 */
	private static boolean hasAdminRight(String page) {
		return page.contains("admin");
	}
	
	/**
	 * Vrati informacie o tom, ci ma EKONOMPRACOVNIK prava na danu akciu
	 * @return true ak ma, inak false
	 */
	private static boolean hasWorkerRight(AccessRight right) {
		switch (right) {
			case SUBJECTS:
			case DISCOUNTS:
			case ORDERS:
			case STOCK_TYPES:
			case STOCK_ITEMS_LIST:
				return true;
		}
		return false;
	}
	
	/**
	 * Vrati informacie o tom, ci ma EKONOMPRACOVNIK prava na danu stranku
	 * @return true ak ma, inak false
	 */
	private static boolean hasWorkerRight(String page) {
		return page.contains("ekonom");
	}
	
	/**
	 * Vrati informacie o tom, ci ma SKLADNIK prava na danu akciu
	 * @return true ak ma, inak false
	 */
	private static boolean hasWarehouseWorkerRight(AccessRight right) {
		switch (right) {
			case STOCK_ITEMS:
			case SLOTS:
			case ORDERS_WHMAN:
				return true;
		}
		return false;
	}
	
	/**
	 * Vrati informacie o tom, ci ma SKLADNIK prava na danu stranku
	 * @return true ak ma, inak false
	 */
	private static boolean hasWarehouseWorkerRight(String page) {
		return page.contains("whman"); 
	}
	
	/**
	 * Otestuje ci ma prihlaseny uzivatel pravo na danu stranku
	 * @param user uzivatel
	 * @param page zobrazovana stranka 
	 * @return true ak ma pravo, false v opacnom pripade
	 */
	public static boolean hasRightToViewPage(User user, String page) {
		if (user == null)
			return false;
		
		// ADMIN
		if (user.getRole().equals("ADM")) {
			return hasAdminRight(page);
		} 
		
		// Ekonomicky pracovnik
		if (user.getRole().equals("PRC")) {
			return hasWorkerRight(page);
		} 
		
		// Skladnik
		if (user.getRole().equals("SKL")) {
			return hasWarehouseWorkerRight(page);
		} 
		
		return false;
	}
}
